package com.ask.service.Impl;

import com.ask.constant.StatusConstant;
import com.ask.dto.EmployeeDTO;
import com.ask.dto.EmployeePageQueryDTO;
import com.ask.dto.EmployeeLoginDTO;
import com.ask.dto.PasswordEditDTO;
import com.ask.entity.Employee;
import com.ask.exception.BaseException;
import com.ask.exception.PasswordEditException;
import com.ask.exception.ResourceNotExistException;
import com.ask.mapper.EmployeeMapper;
import com.ask.result.PageResult;
import com.ask.service.EmployeeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;

@Service
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {
    private static final String defaultPassword = "123456";
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void save(EmployeeDTO employeeDTO) {
        // 1. 属性拷贝
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        // 设置默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex(defaultPassword.getBytes()));
        // 设置创建时间和创建用户
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateUser(1l);

        employee.setStatus(StatusConstant.ENABLE);
        // 插入数据库
        employeeMapper.insert(employee);
    }

    /**
     * 跟新员工
     *
     * @param employeeDTO
     * @return
     */
    @Override
    public Employee update(EmployeeDTO employeeDTO) {
        // 1. 查询该记录是否存在
        Employee item = employeeMapper.getById(employeeDTO.getId());
        if (item == null)
            throw new ResourceNotExistException("员工不存在！");
        // 2. 跟新数据库
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(1L);
        employeeMapper.update(employee);
        return employee;
    }

    /**
     * 根据id获取员工
     *
     * @param id
     * @return
     * @throws Exception
     */
    public Employee getById(@RequestParam Long id) {
        Employee item = employeeMapper.getById(id);
        if (item == null)
            throw new ResourceNotExistException("员工不存在！");
        item.setPassword("******");
        return item;
    }

    /**
     * 启用/禁用员工账号
     *
     * @param id
     * @return
     */
    @Override
    public void startorStop(Integer status, Long id) {
        Employee employee = new Employee();
        employee.setStatus(status);
        employee.setId(id);
        employeeMapper.update(employee);
    }

    /**
     * 修改密码
     *
     * @param passwordEditDTO
     * @return
     */
    @Override
    public void editPassword(PasswordEditDTO passwordEditDTO) {
        // 1. 获取员工信息
        Employee employee = employeeMapper.getById(passwordEditDTO.getEmpId());
        String oldPassword = passwordEditDTO.getOldPassword();
        // 2. 验证旧密码
        if (!DigestUtils.md5DigestAsHex(oldPassword.getBytes()).equals(employee.getPassword()))
            throw new PasswordEditException("旧密码错误！");

        // 3. 更新新密码
        employee.setPassword(DigestUtils.md5DigestAsHex(passwordEditDTO.getNewPassword().getBytes()));
        employeeMapper.update(employee);
    }

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Employee login(EmployeeLoginDTO loginDTO) {
        String password = loginDTO.getPassword();
        Employee employee = employeeMapper.getByUserName(loginDTO.getUsername());
        if (employee == null)
            throw new BaseException("用户不存在！");
        if(!DigestUtils.md5DigestAsHex(password.getBytes()).equals(employee.getPassword()))
            throw new BaseException("密码错误！");
        return employee;
    }
}
